package com.yz.jdd.module.main.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yz.jdd.R;
import com.yz.jdd.module.main.entity.ShopListItem;

import java.util.List;


public class ShopsAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<ShopListItem.ResultObjectBean.PageDataBean> orderListItemList;
    private boolean isSetAct;
    private String from;

    public ShopsAdapter(Context context, List<ShopListItem.ResultObjectBean.PageDataBean> orderListItemList, boolean isSetAct,String from) {
        this.context = context;
        this.orderListItemList = orderListItemList;
        this.isSetAct = isSetAct;
        this.from=from;
    }

    @Override
    public int getGroupCount() {
        return orderListItemList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return orderListItemList.get(i).getShops().size();
    }

    @Override
    public Object getGroup(int i) {
        return orderListItemList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return orderListItemList.get(i).getShops().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    @Override
    public boolean hasStableIds() {
        return true;
    }

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shop_list_partent, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.city_tv);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        //关键字搜索
        boolean isHave = false;
        for (int j = 0; j < orderListItemList.get(i).getShops().size(); j++) {
            if (!orderListItemList.get(i).getShops().get(j).isShow()) {
                isHave = true;
                break;
            }
        }
        if (isHave) {
            groupViewHolder.tvTitle.setText(orderListItemList.get(i).getCityName());
            groupViewHolder.tvTitle.setVisibility(View.VISIBLE);
        } else {
            groupViewHolder.tvTitle.setVisibility(View.GONE);
        }


        return convertView;

    }

    @Override
    public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shop_list, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.allshop_layout = (RelativeLayout) convertView.findViewById(R.id.allshop_layout);
            childViewHolder.view_line = (View) convertView.findViewById(R.id.view_line);
            childViewHolder.tvTitle = (TextView) convertView.findViewById(R.id.shop_name_tv);
            childViewHolder.select_img = (ImageView) convertView.findViewById(R.id.select_img);
            convertView.setTag(childViewHolder);

        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }


//        int status = (int) orderListItemList.get(i).getShops().get(i1).getAuditStatus();
//        switch (status) {
//            case 1:  // 审批中
//                childViewHolder.tvTitle.setText(orderListItemList.get(i).getShops().get(i1).getShopName() + " 审批中");
//                break;
//            case 2:  // 变更审批中
//                childViewHolder.tvTitle.setText(orderListItemList.get(i).getShops().get(i1).getShopName() + " 变更审批中");
//                break;
//            case 3:  // 审批驳回
//                childViewHolder.tvTitle.setText(orderListItemList.get(i).getShops().get(i1).getShopName() + " 审批驳回");
//                break;
//            case 4: // 变更审批驳回
//                childViewHolder.tvTitle.setText(orderListItemList.get(i).getShops().get(i1).getShopName() + " 变更审批驳回");
//                break;
//            case 5:  //审批通过
//                childViewHolder.tvTitle.setText(orderListItemList.get(i).getShops().get(i1).getShopName());
//                break;
//            case 6:  //变更审批通过
//                childViewHolder.tvTitle.setText(orderListItemList.get(i).getShops().get(i1).getShopName());
//                break;
//            default:
        childViewHolder.tvTitle.setText(orderListItemList.get(i).getShops().get(i1).getShopName());
//                break;
//        }


        if (orderListItemList.get(i).getShops().get(i1).isCheck()) {
            // 语音设置不显示标记
            if (!isSetAct) {
                childViewHolder.select_img.setVisibility(View.VISIBLE);
            }
//            if(!TextUtils.isEmpty(from)){
//                childViewHolder.select_img.setVisibility(View.VISIBLE);
//            }
            childViewHolder.tvTitle.setTextColor(Color.parseColor("#459dff"));
        } else {
            childViewHolder.select_img.setVisibility(View.GONE);
//            if (status != 5 && status != 6) {
//                childViewHolder.tvTitle.setTextColor(Color.parseColor("#888888"));
//            }else{
            childViewHolder.tvTitle.setTextColor(Color.parseColor("#000000"));
//            }
        }

        //关键字搜索
        if (orderListItemList.get(i).getShops().get(i1).isShow()) {
            childViewHolder.allshop_layout.setVisibility(View.GONE);
            childViewHolder.view_line.setVisibility(View.GONE);
        } else {
            childViewHolder.allshop_layout.setVisibility(View.VISIBLE);
            childViewHolder.view_line.setVisibility(View.VISIBLE);
        }

        // 最后一行隐藏下划线
        if (i1 + 1 == orderListItemList.get(i).getShops().size()) {
            childViewHolder.view_line.setVisibility(View.GONE);
        }
        return convertView;

    }

    static class GroupViewHolder {
        TextView tvTitle;
    }

    static class ChildViewHolder {
        TextView tvTitle;
        ImageView select_img;
        RelativeLayout allshop_layout;
        View view_line;
    }
}
